package com.huaqin.punan.headset;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView mTextView;
	private Button mButton;
	
	private boolean mBluetoothHeadsetStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTextView = (TextView)findViewById(R.id.tv);
		mButton = (Button)findViewById(R.id.btn);
		
		mBluetoothHeadsetStatus = isHeadsetConnected();
		mTextView.setText(""+mBluetoothHeadsetStatus);
		
		mButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				mBluetoothHeadsetStatus = isHeadsetConnected();
				mTextView.setText(""+mBluetoothHeadsetStatus);
			}});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private boolean isHeadsetConnected(){
		boolean connected = true;
		try {
			connected = BluetoothAdapter.
					getDefaultAdapter().
					getProfileConnectionState(android.bluetooth.BluetoothProfile.HEADSET) 
					!= android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
		}catch(Exception e){
			e.printStackTrace();
		}
		return connected;
	}

}
